package entity.flight.impl;

import entity.airline.impl.AirlineImpl;
import entity.employee.Employee;
import entity.flight.Flight;
import entity.plane.Plane;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.validator.constraints.NotEmpty;


@Entity
@Table(name = "flight")
public class FlightImpl implements Flight {

	private Long id;

	Plane plane;

	List<Employee> employees;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "flights")
	private AirlineImpl airline;

	public FlightImpl() {
		this.employees = new ArrayList<Employee>();
	}

	@NotEmpty
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "plane_id")
	public Plane getPlane() {
		return plane;
	}

	@Override
	public void setPlane(Plane plane) {
		this.plane = plane;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "list_employee", 
	joinColumns = { @JoinColumn(name = "fligt_id") },
	inverseJoinColumns = { @JoinColumn(name = "employee_id") })
	public List<Employee> getEmployees() {
		return employees;
	}

	@Override
	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	@Override
	public void addEmployee(Employee employee) {
		if (employees == null)
			this.employees = new ArrayList<Employee>();
		employees.add(employee);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public AirlineImpl getAirline() {
		return airline;
	}

	public void setAirline(AirlineImpl airline) {
		this.airline = airline;
	}
}
